﻿using MullenStudio.Storage;
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MullenStudio.Media.ConcatVideoPlayerSample
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Urls
            // These videos are not segments of a whole video. They are just some irrelevent videos.
            // You can replace it with your video segments.
            string[] urls = new string[]
            {
                "http://smf.blob.core.windows.net/samples/videos/wildlife.mp4",
                "http://smf.blob.core.windows.net/samples/videos/bigbuck.mp4",
            };
            // Length of each segment, need to know beforehand. Not necessarily be the exact video length; can be shorter or longer.
            TimeSpan[] times = new TimeSpan[] { new TimeSpan(0, 0, 30), new TimeSpan(0, 0, 25) };

            // Apply new ViewModel
            VideoPlayer.InteractiveViewModel = VideoPlayer.DefaultInteractiveViewModel = new ConcatInteractiveViewModel(VideoPlayer);

            // Initialize streams
            DownloadRandomAccessStream[] streams = new DownloadRandomAccessStream[urls.Length];
            string[] types = new string[urls.Length];
            for (int i = 0; i < urls.Length; i++)
            {
                streams[i] = new DownloadRandomAccessStream(new Uri(urls[i]));
                await streams[i].InitializeAsync();
                // Set mimetypes
                types[i] = streams[i].Headers.ContentType.MediaType;
            }
            // Set segments
            (VideoPlayer.InteractiveViewModel as ConcatInteractiveViewModel).SetSegments(streams, types, times);
            // If you want to play local video files, just use IRandomAccessStream[] with file streams (StorageFile.OpenAsync) for segmentStreams, and ContentType for mimeTypes
            // Can use StorageFile.Properties.GetVideoProertiesAsync to get VideoProperties, and VideoProperties.Duration for durations.
        }
    }
}
